<template>
  <div class="implicit-loading" v-loading.fullscreen.lock="loading" element-loading-text="加载中..."
    element-loading-background="rgba(0, 0, 0, 0.7)">
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { usePermissStore } from '../store/permiss';
import { useUserStore } from '../store/user';
import { useRouter } from 'vue-router';
import { getLogin, getCurUserInfo, getServiceToken } from '../api/index';
const permiss = usePermissStore();
const userStore = useUserStore();
const router = useRouter();
let loading = ref(false);
const Login = async (code, appid, loginState) => {
  let { serviceId = 'sso', state = '', redirectUri = '' } = JSON.parse(localStorage.getItem('authorize_obj')) || {};
  loading.value = true;
  let res = await getLogin({
    code, appid, state: loginState, systemCode: serviceId
  })
  if (res.code === 200) {
    localStorage.setItem('token', res.data)
    userStore.handleSetToken(res.data)
    let res1 = await getCurUserInfo();
    if (res1.code === 200) {
      loading.value = false;
      let keys;
      if(res1.data?.roleType !== 1){
        keys = permiss.defaultList['user'];
      }else{
        keys = permiss.defaultList['admin'];
      }
      permiss.handleSet(keys);
      userStore.handleSetUser(res1.data)
      localStorage.setItem('ms_keys', JSON.stringify(keys));
      localStorage.setItem('ms_user', JSON.stringify(res1.data));
      router.push('/portal');
    }
  }
}
onMounted(() => {
  let route = router.currentRoute.value
  Login(route.query.code, route.query.appid, route.query.state)
})
</script>

<style scoped>
.implicit-loading {
  width: 100vw;
  height: 100vh;
}
</style>
